package team.pikachu.pikachuclient.message;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import team.pikachu.pikachuclient.R;
import team.pikachu.pikachuclient.beans.Trouble;
import team.pikachu.pikachuclient.utils.Constants;
import team.pikachu.pikachuclient.utils.HttpUtils;
import team.pikachu.pikachuclient.utils.IPConstants;
import team.pikachu.pikachuclient.utils.JsonUtils;
import team.pikachu.pikachuclient.utils.LogUtil;


/**
 * 故障展示页面
 * 展示已上传的维修任务的具体情况
 */
public class MsgItemFragment extends Fragment {

    private int roleId;

    private static final String LOGTAG = LogUtil.makeLogTag(MsgItemFragment.class);

    private static MsgItemFragment msgItemFragment = null;

    private SharedPreferences pre;

    private RecyclerView recyclerView;

    private int status = Constants.OP_REPIRE;

    public MsgItemFragment() {
    }

    @SuppressWarnings("unused")
    public static MsgItemFragment newInstance() {
        return msgItemFragment = msgItemFragment == null ? new MsgItemFragment() : msgItemFragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_item_list, container, false);

        // Set the adapter
        Context context = view.getContext();
        recyclerView = (RecyclerView) view;
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.e(LOGTAG, "onResume");
        MsgAsy msgAsy = new MsgAsy();
        String troublePath;
        if(roleId !=  Constants.ROLE_STUDENT && roleId != Constants.ROLE_TEACHER){
            troublePath = IPConstants.getDoing(pre.getInt(getString(R.string.pre_userid),0),status);
        }else {
            troublePath = IPConstants.GET_TROUBLES + pre.getInt(getString(R.string.pre_userid), 0);
        }
        msgAsy.execute(troublePath);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        pre = context.getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        roleId = pre.getInt(getString(R.string.pre_roleid),1);
    }


    private class MsgAsy extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            return HttpUtils.sendPostMethod(params[0]);
        }

        @Override
        protected void onPostExecute(String s) {
//            Log.e(LOGTAG,s);
            if (!isAdded())
                return;
            if (s.equals(getString(R.string.timeout))) {
                return;
            }
            if (!s.equals(getString(R.string.request_failed))) {
                Trouble[] troubles = JsonUtils.readValue(s, Trouble[].class);
                for (Trouble t : troubles) {
                    Log.i(LOGTAG, t.toString());
                }
                List<Trouble> troubleList = Arrays.asList(troubles);
                Collections.reverse(troubleList);
                MyItemRecyclerViewAdapter adapter = new MyItemRecyclerViewAdapter(troubleList);
                adapter.setOnItemClickListener(new MyItemRecyclerViewAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(Trouble trouble) {
                        Log.d(LOGTAG, trouble.toString());
                        Intent intent = new Intent(getActivity(), MsgDetailActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("TROUBLE", trouble);
                        intent.putExtras(bundle);
                        startActivity(intent);
                        bundle.clear();
                    }

                    @Override
                    public void onItemLongClick(Trouble trouble) {
                        Log.d(LOGTAG, trouble.toString());
                        Intent intent = new Intent(getActivity(), MsgDetailActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("TROUBLE", trouble);
                        intent.putExtras(bundle);
                        startActivity(intent);
                        bundle.clear();
                    }
                });
                recyclerView.setAdapter(adapter);
            } else {
                recyclerView.setAdapter(new MyPromptAdapter(pre.getInt(getString(R.string.pre_roleid),0)));
            }
        }
    }

    public void onUpdate(int status){
//        Log.d(LOGTAG,status + "");
        this.status = status;
        MsgAsy msgAsy = new MsgAsy();
        String troublePath = IPConstants.getDoing(pre.getInt(getString(R.string.pre_userid),0),status);
        msgAsy.execute(troublePath);
    }
}
